package com.zzb.framework.log.utils;

import com.zzb.framework.common.ServiceException;
import com.zzb.framework.log.entity.SysLog;
import com.zzb.framework.log.enums.LogLevelEnum;
import com.zzb.framework.log.service.SysLogService;
import com.zzb.framework.role.entity.IUser;
import com.zzb.framework.user.components.AbstractUserManager;
import com.zzb.framework.user.utils.SystemUser;
import com.zzb.framework.utils.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;

/**
 * 日志
 * Created by zhangzhenbin on 17-5-8.
 */
public class Log {
    private final static Logger logger = LoggerFactory.getLogger(Log.class);
    private static SysLogService sysLogService = null;
    private static AbstractUserManager userManager = null;
    static {
        sysLogService =  SpringContextHolder.getBean("sysLogService");
        userManager = SpringContextHolder.getBean("frameworkUserManager");
    }
    public static SysLog getTraceInfo(int level){
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        SysLog sysLog = new SysLog();
        sysLog.setClassName(stacks[level].getClassName());
        sysLog.setLineNumber(stacks[level].getLineNumber());
        sysLog.setMethodName(stacks[level].getMethodName());
        return sysLog;
    }
    public static int getTraceInfoLevel(){
        StackTraceElement[] stacks = new Throwable().getStackTrace();
        return stacks.length;
    }
    private static void saveLog(String msg, LogLevelEnum logLevelEnum,String logType){
        SysLog sysLog = null;
        try {
            sysLog = getTraceInfo(3);
            sysLog.setLogContent(msg);
            sysLog.setLogLevel(logLevelEnum.name());
            sysLog.setLogType(logType);
            IUser currentUser = SystemUser.root();
            sysLogService.saveSysLog(sysLog,currentUser);
        } catch (ServiceException e) {
            logger.error(e.getMessage());
        }
        if(logLevelEnum.equals(LogLevelEnum.DEBUG)){
            logger.debug(sysLog.toString());
        } else if( logLevelEnum.equals(LogLevelEnum.INFO)){
            logger.info(sysLog.toString());
        } else if(logLevelEnum.equals(LogLevelEnum.WARN)){
            logger.warn(sysLog.toString());
        } else {
            logger.error(sysLog.toString());
        }
    }
    public static void debug(String msg){
        saveLog(msg,LogLevelEnum.DEBUG,"");
    }
    public static void debug(String type,String msg){
        saveLog(msg,LogLevelEnum.DEBUG,type);
    }

    public static void debug(String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.DEBUG,"");
    }
    public static void debug(String type ,String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.DEBUG,type);
    }
    public static void info(String msg){
        saveLog(msg,LogLevelEnum.INFO,"");
    }
    public static void info(String type,String msg){
        saveLog(msg,LogLevelEnum.INFO,type);
    }

    public static void info(String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.INFO,"");
    }
    public static void info(String type ,String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.INFO,type);
    }
    public static void warn(String msg){
        saveLog(msg,LogLevelEnum.WARN,"");
    }
    public static void warn(String type,String msg){
        saveLog(msg,LogLevelEnum.WARN,type);
    }

    public static void warn(String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.WARN,"");
    }
    public static void warn(String type ,String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.WARN,type);
    }
    public static void error(String msg){
        saveLog(msg,LogLevelEnum.ERROR,"");
    }
    public static void error(String type,String msg){
        saveLog(msg,LogLevelEnum.ERROR,type);
    }

    public static void error(String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.ERROR,"");
    }
    public static void error(String type ,String msg, Object ... objects){
        String formatMsg = String.format(msg, objects);
        saveLog(formatMsg,LogLevelEnum.ERROR,type);
    }

}
